﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Lottery.Client.SL.Controls;

namespace Lottery.Client.SL
{
    public partial class SystemMain : UserControl,ISystemMain
    {
        private bool _unknown1;
        private RefreshLogin _refreshLogin;
        private bool _unknown2;
        private int _loginTimes;
        public static int IsShow { get; private set; }
        public SystemMain()
        {
            InitializeComponent();
            PreProcess();
        }
        static SystemMain()
        {
            SystemMain.IsShow = 2;
        }

        public IBuyLottery IBuy { get; set; }

        public void PreProcess()
        {
            _refreshLogin = new RefreshLogin();
            base.Loaded+=(new RoutedEventHandler(this.Base_Loaded));
            btnPoint.MouseLeftButtonDown += btnPoint_MouseLeftButtonDown;
            Minimized.MouseLeftButtonDown += Minimized_MouseLeftButtonDown;
            CvSoundImg.MouseLeftButtonDown += CvSoundImg_MouseLeftButtonDown;
            btnClose.MouseLeftButtonDown += btnClose_MouseLeftButtonDown;
            btnAddMoney.MouseLeftButtonDown += btnAddMoney_MouseLeftButtonDown;
            btnWithdraw.MouseLeftButtonDown += btnWithdraw_MouseLeftButtonDown;
            ToolBar.MouseLeftButtonDown += ToolBar_MouseLeftButtonDown;
            lb_connect.MouseLeftButtonDown += lb_connect_MouseLeftButtonDown;
            this.DataContext = DataCache.CurrentUserInfo;
            //this.fAapga49Qf(null);
        }

        private void Base_Loaded(object sender, RoutedEventArgs e)
        {
            this.ChangeSelectedElementStyle(this.Menu.Children[1]);
            this.LoadMenuContent("0");
            this.SetMenuElementTagAndEventsHandlers();
            this.SetReplicate(true);
        }
        private void SetMenuElementTagAndEventsHandlers()
        {
            int num = 0;
            using (IEnumerator<UIElement> enumerator = this.Menu.Children.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    UIElement current = enumerator.Current;
                    if (current.GetType() == (typeof(TextBlock)))
                    {
                        //Tag:序号 0 1 2 ...
                        ((TextBlock)current).Tag = (num.ToString());
                        current.MouseLeftButtonDown+=(new MouseButtonEventHandler(this.CheckAgentPermisson));
                        num++;
                    }
                }
            }
        }
        /// <summary>
        /// 验证代理使用权限并打开相应窗口
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="mouseButtonEventArgs"></param>
        private void CheckAgentPermisson(object obj, MouseButtonEventArgs mouseButtonEventArgs)
        {
            this.ChangeSelectedElementStyle(obj);
            string text = ((TextBlock)obj).Tag.ToString();
            var debug = true;
            if (!debug)
            {

                if (text == "2" && !PublicFunction.CheckAgent(""))
                {
                    App.ShowMsg("您无法使用该权限！");
                    return;
                }
            }
            this.LoadMenuContent(text);
        }

        private void ChangeSelectedElementStyle(object obj)
        {
            int num = 0;
            using (IEnumerator<UIElement> enumerator = this.Menu.Children.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    UIElement current = enumerator.Current;
                    if (current is TextBlock)
                    {
                        if ((UIElement)obj == current)
                        {
                            this.SelectItem.SetValue(Canvas.LeftProperty, (double)num * 70.0);
                            current.SetValue(Canvas.LeftProperty, (double)num * 70.0 + 5.6);
                            current.SetValue(Canvas.TopProperty, 9.0);
                            ((TextBlock)current).Style = ((Style) base.Resources["MenuSelectItemFontStyle1"]);
                        }
                        else
                        {
                            current.SetValue(Canvas.LeftProperty, (double)num * 70.0 + 7.5);
                            current.SetValue(Canvas.TopProperty, 9.0);
                            ((TextBlock)current).Style = ((Style) base.Resources["MenuNormalItemStyle1"]);
                        }
                        num++;
                    }
                }
            }
        }
        private void LoadMenuContent(string text)
        {
            this.MenuContent.Children.Clear();
            if (text != null)
            {
                if (text == "0")
                {
                    LotteryControlInfo lotteryControlInfo = new LotteryControlInfo();
                    lotteryControlInfo.IMain = this;
                    this.MenuContent.Children.Add(lotteryControlInfo);
                    return;
                }
                if (text == "1")
                {
                    UserCenter userCenter = new UserCenter();
                    userCenter.IMain = this;
                    this.MenuContent.Children.Add(userCenter);
                    this.LoadNews();
                    return;
                }
                if (text != "2")
                {
                    return;
                }
                AgentCenter agentCenter = new AgentCenter();
                agentCenter.IMain = this;
                this.MenuContent.Children.Add(agentCenter);
                this.LoadNews();
            }
        }

        public void SetLotteryLabel()
        {
            if (this.IBuy == null)
            {
                this.LoadBuyForm();
            }
            this.IBuy.SetGameMenu();
            this.IBuy.OpenChaseInfo();
        }
        private void lb_connect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //if (MessageBox.Show(MsgInfo.logoutInfo, MsgInfo.mestitle, 1) == 1)
            //{
            //    this.bt_exit_Click(this, null);
            //}
            if(MessageBox.Show("确定注销登录吗?") == MessageBoxResult.Yes)
            {
               // this.btn
               //清理Cache，退出当前窗口
            }
        }
        public void bt_exit_Click(object sender, RoutedEventArgs e)
        {
            this.LoadNews();
            if (DataCache.ChildWindowObject != null)
            {
                ChildWindow childWindow = DataCache.ChildWindowObject as ChildWindow;
                if (childWindow != null)
                {
                    childWindow.Close();
                }
            }
            DataCache.Clear();
            this._refreshLogin.Closed += new EventHandler(this.aS4pDwe5us);
            this._refreshLogin.ShowRefreshLogin(SystemMain.IsShow);
        }
        private void aS4pDwe5us(object obj, EventArgs eventArgs)
        {
            if (this._refreshLogin.DialogResult.HasValue)
            {
                this._loginTimes = 0;
                this.LogOut(null);
                return;
            }
            Application.Current.MainWindow.Close();
        }
        private void LogOut(object obj)
        {
            //App.app_client.PropertyChanged += new PropertyChangedEventHandler(this.hQrpuK4L8w);
            //App.app_client.UserInfoChanged += new EventHandler(this.GoEpYEfhag);
            //Binding binding = new Binding(BcZJxKMIDcH0kvSFls.w4WekCpilj(20720));
            //binding.set_Converter(new ConnectedFormatConvert());
            //binding.set_Source(App.app_client);
            //this.lb_connect.SetBinding(TextBlock.TextProperty, binding);
            //binding = new Binding(BcZJxKMIDcH0kvSFls.w4WekCpilj(20742));
            //binding.set_Converter(new ConnectedColorConvert());
            //binding.set_Source(App.app_client);
            //this.lb_connect.SetBinding(TextBlock.ForegroundProperty, binding);
            this.ResetUserInfoControl();
        }

        private void ResetUserInfoControl()
        {
            this.tbName.Text = DataCache.CurrentLoginInfo.UserName;
            this.tbLevel.Text = DataCache.CurrentUserInfo.UserTypeName;
            this.tbBalance.Text = (DataCache.CurrentUserInfo.Balance.ToString(CultureInfo.InvariantCulture) + DataCache.Units);
            this.tbFreeze.Text = (DataCache.CurrentUserInfo.Freeze.ToString(CultureInfo.InvariantCulture) + DataCache.Units);
            this.lbService.NavigateUri = (new Uri("http://Myservice" + DataCache.CurrentLoginInfo.UserName));
        }
        private void ToolBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Window mainWindow = Application.Current.MainWindow;
            mainWindow.DragMove();
        }

        private void btnWithdraw_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.SetReplicate(true);
            RefreshNews();
            if (DataCache.CurrentUserBankInfo == null)
            {
                SetUserInfo setUserInfo = new SetUserInfo();
                setUserInfo.Show();
                return;
            }
            UserRecharge userRecharge = new UserRecharge();
            userRecharge.Show();
        }

        private void btnAddMoney_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.SetReplicate(true);
            this.RefreshNews();
            if (DataCache.CurrentUserBankInfo == null)
            {
                SetUserInfo setUserInfo = new SetUserInfo();
                setUserInfo.Show();
                return;
            }
            UserRecharge userRecharge = new UserRecharge();
            userRecharge.Show();
        }

        private void RefreshNews()
        {
            if (this.CvBuyLottery.Children.Count == 0)
            {
                this.LoadNews();
            }
        }

        private void btnClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (MessageBox.Show("确定要退出吗?") == MessageBoxResult.OK)
            {
                Application.Current.MainWindow.Close();
            }
        }

        private void CvSoundImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (DataCache.RealSound)
            {
                DataCache.RealSound = false;
                this.CvSoundMute.Visibility = Visibility.Visible;
                return;
            }
            DataCache.RealSound = true;
            this.CvSoundMute.Visibility = Visibility.Collapsed;
        }

        private void Minimized_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;
        }

        private void btnPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.MainWindow.Width == 260.0)
            {
                if (CvBuyLottery.Children.Count == 0)
                {
                    LoadNews();
                }
                SetReplicate(true);
                return;
            }
            SetReplicate(false);
        }

        public void SetReplicate(bool isShow)
        {
            if (isShow)
            {
                if (Application.Current.MainWindow.Width != 980.0)
                {
                    Application.Current.MainWindow.Width = 980.0;
                    ((UserControl) base.Parent).Width = 980.0;
                    ToolBar.Width = 980.0;
                   // var rotateTransform = new RotateTransform();
                   // rotateTransform.Angle = 180.0;
                  //  btnPoint.RenderTransform = rotateTransform;
                    return;
                }
            }
            else
            {
                if (Application.Current.MainWindow.Width != 260.0)
                {
                    Application.Current.MainWindow.Width = 260.0;
                    ((UserControl) base.Parent).Width = 260.0;
                    ToolBar.Width = 260.0;
                  //  var rotateTransform2 = new RotateTransform();
                  //  rotateTransform2.Angle = 0.0;
                  //  btnPoint.RenderTransform = rotateTransform2;
                }
            }
        }

        private void LoadNews()
        {
            CvBuyLottery.Children.Clear();
            var news = new News();
            LoadUserControl(news);
            _unknown1 = false;
        }

        public void LoadUserControl(object obj)
        {
            this.SetReplicate(true);
            this.CvBuyLottery.Children.Clear();
            this.CvBuyLottery.Children.Add((UserControl)obj);
        }

        public void LoadBuyForm()
        {
            //\\
            //if (this._unknown2) return;
            BuyLottery buyLottery = new BuyLottery();
            this.IBuy = buyLottery.IBuy;
            this.IBuy.SetInterfaceValue(this);
            this.LoadUserControl(buyLottery);
            this._unknown2 = true;
        }
    }
}